/**
* Created by XHL on 2022-07-23.
*/

#include <iostream>
#include <common.h>

namespace handle {

	void wasd(Location &location, const int &key, Enum::Direction &direction) {
		switch (key) {
			case 'w':
				direction = Enum::D_UP;
				location += { 0, -1 };
				break;
			case 's':
				direction = Enum::D_DOWN;
				location += { 0, 1 };
				break;
			case 'a':
				direction = Enum::D_LEFT;
				location += { -2, 0 };
				break;
			case 'd':
				direction = Enum::D_RIGHT;
				location += { 2, 0 };
				break;
			default:
				break;
		}
	}

	void arrow(Location &location, const int &key, Enum::Direction &direction) {
		switch (key) {
			case 72:
				direction = Enum::D_UP;
				location += { 0, -1 };
				break;
			case 80:
				direction = Enum::D_DOWN;
				location += { 0, 1 };
				break;
			case 75:
				direction = Enum::D_LEFT;
				location += { -2, 0 };
				break;
			case 77:
				direction = Enum::D_RIGHT;
				location += { 2, 0 };
				break;
			default:
				break;
		}
	}

	void set_cursor_location(const Location &pos) {
		/** HIDE CURSOR */
		std::cout << "\033[?25l";
		/** SET NEW CURSOR LOCATION,<br>
		 * \033[y;xH moves cursor to (x, y)
		 * where x is the column of the y line
		 * */
		tool::format << "\033[" << pos.get_y() << ";" << pos.get_x() << "H";
		std::cout << tool::format.str();
		tool::format.clear();
	}
}

namespace global {
	/** game_round starting from 1 to reduce lots of judges */
	int game_round = 1;
	/** location of the map */
	meta::Location left_top{ 10, 5 };// NOLINT

	const char *ESC = "\033[0m";

	const char *shape_h = "-";
	const char *shape_v = ".";
	const char *shape_n = nullptr;

	std::string end[] = { "\033[1;37;41mU\033[0m",// NOLINT
						  "\033[1;37;41mD\033[0m",
						  "\033[1;37;41mL\033[0m",
						  "\033[1;37;41mR\033[0m" };
}

namespace tool {

	std::stringstream format; //NOLINT
}
